var vum = new Vue({
el: '#app-home',
data: {
    hotData:[],
    hotDataOne:[],
    hotDataTwo:[],
    newData:[],
    newDataOne:[],
    newDataTwo:[],
    newDataThree:[],
    firstCatData:[],
    firstCatShowData:[],
    firstCatHideData:[],
    layerOneCat:[],
    layerOneList:[],
    layerTwoCat:[],
    layerTwoList:[],
    layerThreeCat:[],
    layerThreeList:[],
    layerFourList:[],
    shoppList: [],
    getSize: '',
    totalPriceTop:0,
    carId: '',
	carNum: '',
    copeWith: '',
    prepaid: '',
	changeMoney: '',
    count:0,  //判断选中多少商品的个数
    status:false,//全选选中状态
    allchoose:0,//店铺选中个数
    allsum:0,//总计价格
    allnum:0,//总计数量

},
filters: {
  priceOld: function (value) {
    if (!value) return ''
    value = value*1.3;
	var regex = /^\+?[1-9][0-9]*$/;
	if(regex.test(value)) {
	  value = parseFloat(value);
	}
	else{
	  var percentVal =  Number(parseFloat(value).toFixed(4).slice(0,-2));
	  var s = percentVal.toString();
	  var rs = s.indexOf('.');
	  if (rs < 0) {
	    rs = s.length;
	    s += '.';
	  }
	  // while (s.length <= rs + 2) {
	  //   s += '';
	  // }
	  value = s;
	}
	value = value;
    return value
  }
},
created(){
	this.firstCat();
	this.hotShop();
	this.newShop();
	this.catalogList();
	this.shopCartList();
    this.prepaid = localStorage.getItem('__USERPRICE_'); // 预付
    this.copeWith = localStorage.getItem('totalNum'); //应付
    console.log(this.prepaid)
},
mounted(){
},
computed: {
    // totalCount:function(){
    //     let totalCount=0;
    //     vum.carNum = '';
    //     for(let i=0;i<vum.shoppList.length;i++){
    //     		console.log(333)
    //     		totalCount += Number(vum.shoppList[i].item_number);
    //             vum.carNum += parseFloat(vum.shoppList[i].item_number)+','
    //             console.log(vum.carNum)
	//
    //     }
    //     return totalCount.toString().replace(/\B(?=(\d{3})+$)/g,',');
    // },
    // totalPrice:function(){
    //     let totalPrice = 0;
    //     for(let i=0;i<vum.shoppList.length;i++){
    //         totalPrice += vum.shoppList[i].item_number * vum.shoppList[i].price;
    //         localStorage.setItem('totalNum', totalPrice);
    //     }
    //     return totalPrice;
    // }
},
watch:{
},
methods:{
	goCart(val){
		// console.log(val);
		window.location.href="/product.html?Id="+val;
	},
	firstCat(){
		$.ajax({
			url:doMain3+'/goods/itemCatList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			success:function (datas) {
				// console.log('一级分类');
				// console.log(datas);
				vum.firstCatData = datas.resp;
				var lenFirstCat = vum.firstCatData.length;
				vum.firstCatShowData = vum.firstCatData.slice(0,13);
				vum.firstCatHideData = vum.firstCatData.slice(13,lenFirstCat);
			},
			error: function(res) {
				console.log('失败');
			},
		})
	},
	hotShop(){
		var dataHost = {
			limit: 8,
			page: 1,
			sign: "string",
		};
		$.ajax({
			url:doMain3+'/goods/zitemHotList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataHost),
			success:function (datas) {
				// console.log('热卖商品');
				vum.hotData = datas.resp;
				vum.hotDataOne = vum.hotData.slice(0,4);
      			vum.hotDataTwo = vum.hotData.slice(4,8);
			},
			error: function(res) {
				console.log('失败');
			},
		})
	},
	newShop(){
		var dataNew = {
			limit: 8,
			page: 1,
			shopType: "1",
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew),
			success:function (datas) {
				// console.log('最新商品');
				// console.log(datas);
				vum.newData = datas.resp;
				vum.newDataOne = vum.newData.slice(0,4);
				vum.newDataTwo = vum.newData.slice(4,8);
				vum.newDataThree = vum.newData.slice(4,6)
			},
			error: function(res) {
				console.log('失败');
			},
		})
	},
	catalogList(){
		// 首页楼层1
		var dataAddCart1 = {
			id: 580,
			sign: "string"
		};
		$.ajax({
			url:doMain3+'/goods/itemTwoCats',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart1),
			success:function (datas) {
				// console.log('数码580');
				// console.log(datas);
				vum.layerOneCat = datas.resp;
			},
			error: function(res) {
				console.log('失败');
			},
		});
		var dataNew1 = {
			cid_one: 580,
			limit: 10,
			page: 1,
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew1),
			success:function (datas) {
				// console.log('数码商品列表');
				// console.log(datas);
				var layerOneData = datas.resp;
				vum.layerOneList = layerOneData.slice(0,4);
			},
			error: function(res) {
				console.log('失败');
			},
		});
		// 首页楼层2
		var dataAddCart2 = {
			id: 633,
			sign: "string"
		};
		$.ajax({
			url:doMain3+'/goods/itemTwoCats',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart2),
			success:function (datas) {
				// console.log('数码580');
				// console.log(datas);
				vum.layerTwoCat = datas.resp;
			},
			error: function(res) {
				console.log('失败');
			},
		});
		var dataNew2 = {
			cid_one: 633,
			limit: 10,
			page: 1,
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew2),
			success:function (datas) {
				// console.log('数码商品列表');
				// console.log(datas);
				var layerOneData = datas.resp;
				vum.layerTwoList = layerOneData.slice(0,4);
			},
			error: function(res) {
				console.log('失败');
			},
		});
		// 首页楼层3
		var dataAddCart3 = {
			id: 749,
			sign: "string"
		};
		$.ajax({
			url:doMain3+'/goods/itemTwoCats',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart3),
			success:function (datas) {
				// console.log('数码580');
				// console.log(datas);
				vum.layerThreeCat = datas.resp;
			},
			error: function(res) {
				console.log('失败');
			},
		});
		var dataNew3 = {
			cid_one: 749,
			limit: 10,
			page: 1,
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew3),
			success:function (datas) {
				// console.log('数码商品列表');
				// console.log(datas);
				var layerOneData = datas.resp;
				vum.layerThreeList = layerOneData.slice(0,4);
			},
			error: function(res) {
				console.log('失败');
			},
		});

		// 首页楼层4
		var dataNew4 = {
			cid_one: 865,
			limit: 10,
			page: 1,
			sign: "string",
			user_id: 0
		};
		$.ajax({
			url:doMain3+'/goods/preferentialArea',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataNew4),
			success:function (datas) {
				// console.log('数码商品列表');
				// console.log(datas);
				var layerOneData = datas.resp;
				vum.layerFourList = layerOneData.slice(0,4);
			},
			error: function(res) {
				console.log('失败');
			},
		});



		// layerOneList:[],
	    // layerTwoList:[],
	    // layerThreeList:[],

	},
	shopCartList(){
		var userId=localStorage.__USERMALLID__||'';
		console.log(parseInt(userId));
		var dataAddCart = {
			limit: 20,
			page: 1,
			sign: "string",
			user_id: '8822062'
		};
		// $('.loading').fadeIn();
		$.ajax({
			// url:doMain3+'/goods/zshoppingTrolleyList',
            url:doMain3+'/goods/zshoppingTrolleyList',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataAddCart),
			success:function (datas) {
				console.log('购物车列表');
				vum.shoppList = datas.resp
				console.log(vum.shoppList)
				vum.carId=''
				for (var i=0 ;i< vum.shoppList.length; i++){
                    Vue.set(vum.shoppList[i],'check',false) //计算总和到list中
					let lists =  vum.shoppList[i].list
					for (var z=0;z<lists.length;z++){
						// if (lists[z].checked === true){
                        //     vum.carId+=lists[z].shoppingTrolley_id+','
						// }
						let sum = Number(lists[z].item_number * lists[z].price)
                        Vue.set(lists[z],'sum',sum) //计算总和到list中
                        Vue.set(lists[z],'checked',false)
					}
                 }
			},
			error: function(res) {
				console.log('失败');
			},
		})
	},

    //点击加 增加
    add(pro) {
        vum.carId = ''
        pro.item_number++
        pro.checked ? vum.allnum++ : ''
        pro.sum += pro.price
        pro.checked ? vum.allsum += pro.price : vum.allsum
    }
    ,
	// 点击减
    reduce(pro){
        if(pro.item_number===1){
            pro.item_number
        }else{
            pro.item_number--
            pro.checked?vum.allnum--:''
            pro.sum-=pro.price
            pro.checked?vum.allsum-=pro.price:vum.allsum
        }
	},
	// 输入框
    calculate(pro){
        let oldsum=pro.sum//之前的总价
        let oldnum=oldsum/pro.price//之前的数量
        pro.item_number=parseInt(pro.item_number)//现在的数量
        pro.item_number>0?pro.sum=pro.item_number*pro.price:pro.item_number=oldnum//如果输入数量大于0，计算价格，否则返回之前的数量
        let diffsum=pro.sum-oldsum//差价
        let diffnum=pro.num-oldnum//差量
        if(pro.checked){//如果商品被选中
            vum.allsum+=diffsum//计算总价
            vum.allnum+=diffnum//计算总量
        }
	},
	//全选
    cartchoose(){
        vum.status=!vum.status//取反改变状态
        vum.status?vum.shoppList.forEach((item)=>{this.shoptrue(item)}):vum.shoppList.forEach((item)=>this.shopfalse(item))//根据取反后的状态进行相应的店铺按钮操作
    },
	//商家选中
    shopchoose(item){
		console.log(item,'选中')
        !item.check?this.shoptrue(item):this.shopfalse(item)
    },
	//商家反选
    shopfalse(item){
		console.log(item)
        item.list.forEach((pro)=>{
            console.log(pro)
            pro.checked==true?this.choosefalse(item,pro):''//循环店铺中的商品，先筛选出目前被选中的商品，给它执行choosefalse函数
        })
    },
    shoptrue(item){
		console.log(item)
        item.list.forEach((pro)=>{
            pro.checked===false?this.choosetrue(item,pro):''//循环店铺中的商品，先筛选出目前没选中的商品，给它执行choosetrue函数
        })
    },

	// 商品选中
    choose(item,pro){
		!pro.checked?this.choosetrue(item,pro):this.choosefalse(item,pro)
    },
    choosetrue(item,pro){
		console.log(pro)
        pro.checked=true//将商品选中状态改为true
        ++item.choose===item.list.length?item.check=true:''//这里执行了两部，选中商品数量先+1，再与该店铺商品数量比较，如果相等就更改店铺选中状态为true
        item.check?++vum.allchoose===vum.shoppList.length?vum.status=true:vum.status=false:''//如果店铺选中状态改为true，选中店铺数量先+1，再与店铺数量比较，如果相等就更改全选选中状态为true
        vum.allsum+=parseFloat(pro.sum)
        vum.allnum+=parseInt(pro.item_number);
        // vum.carId = ''
        // item.list.forEach((item)=>{
        // 	if (item.checked == true) {
        //         vum.carId+=item.shoppingTrolley_id+',';
		// 	}
		// 	console.log(vum.carId.substring(0,vum.carId.length-1))
		// })
    },
    choosefalse(item,pro){
        pro.checked=false//将商品选中状态改为false
        --item.choose//选中商品数量-1
        if(item.check){//如果店铺是被选中的，更改店铺选中状态
            item.check=false
            --vum.allchoose//并且选中店铺数量-1
        }
        vum.status=false//无论之前全选的状态，将其改为false就行
        vum.allsum-=Number(pro.sum)//商品总计价格变动
        vum.allnum-=pro.item_number
    },

    //删除数据
    delteNum(id,index) {
     console.log(id)
     console.log(index)
        var dataDeleteCart = {
      		id: id,
            sign: "string",
        };
        // $('.loading').fadeIn();
        $.ajax({
            url:doMain3+'/goods/delShoppingTrolley',
            type:"post",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify(dataDeleteCart),
            success:function (datas) {
                console.log(datas);
                if (datas.code == 10000) {
                    alert(datas.msg)
                    window.location.reload()
                }
            },
            error: function(res) {
             console.log(res)
             alert(res.responseJSON.msg)

            },
        })
    },
    // // 结算
    settlement(){
        vum.carId=''
        vum.carNum=''
        for (var i=0 ;i< vum.shoppList.length; i++){
            let lists =  vum.shoppList[i].list
            for (var z=0;z<lists.length;z++){
                if (lists[z].checked == true){
                    vum.carId+=lists[z].shoppingTrolley_id+','
                    vum.carNum+=lists[z].item_number+','
                    console.log(vum.carNum.substring(0,vum.carNum.length-1))
                }
            }
        }
     let dataVal = {
      id: vum.carId.substring(0,vum.carId.length-1),
      num:vum.carNum.substring(0,vum.carNum.length-1),
     }
     console.log(dataVal)
        // $('.loading').fadeIn();
     $.ajax({
            url:doMain3+'/goods/updateShopping',
                type:"post",
                dataType:'json',
                contentType: "application/json",
                data:JSON.stringify(dataVal),
                success:function (datas) {
                console.log(datas);
                if (datas.code == 10000) {
                 window.location.href='./shopping_closed_goods.html'
       }
            },
            error: function(res) {
                console.log(res)
                alert(res.responseJSON.msg)

            },
        })
    },
   // 批量删除
    AllDetel () {
        vum.carId=''
        for (var i=0 ;i< vum.shoppList.length; i++){
            let lists =  vum.shoppList[i].list
            for (var z=0;z<lists.length;z++){
                if (lists[z].checked == true){
                    vum.carId+=lists[z].shoppingTrolley_id+','
                }
            }
        }
        let dataAll ={
        	id: vum.carId.substring(0,vum.carId.length-1),
			sign: 'string'
        }
        $.ajax({
            url:doMain3+'/goods/delMoreShoppingTrolley',
            type:"post",
            dataType:'json',
            contentType: "application/json",
            data:JSON.stringify(dataAll),
            success:function (datas) {
                console.log(datas);
                if (datas.code == 10000) {
                    alert(datas.msg)
                    window.location.reload()
                }
            },
            error: function(res) {
                alert(res.responseJSON.msg)

            },
		})
    },
	// 支付
	payMent() {
         console.log(vum.carId.substring(0,vum.carId.length-1))
         localStorage.setItem('id',vum.carId.substring(0,vum.carId.length-1))
         let dataPay ={
          addres_id: 361000,
          id: vum.carId.substring(0,vum.carId.length-1),
          sign: 'string'
         }
         $.ajax({
			url:doMain2+'/order/insertOrder',
			type:"post",
			dataType:'json',
			contentType: "application/json",
			data:JSON.stringify(dataPay),
			success:function (datas) {
				console.log(datas);
				if (datas.code == 10000) {
					alert(datas.msg)
				window.location.href='./shopping_pay_way.html?id='+datas.resp

				}
			},
             error: function(res) {
				 console.log(res)
				 alert(res.responseJSON.msg)

			 },
		 })
	}

}
})
